package com.eveinfo.display;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.eveinfo.R;
import com.eveinfo.display.planner.PlannerManager;
import com.eveinfo.display.planner.PlannersAdapter;
import com.eveinfo.tools.ActivityLauncher;
import com.eveinfo.tools.EveInfoMenu;

public class PlannersView extends Activity {

	protected static final int DIALOG_ADD = 0;

	private PlannerManager plannerManager;
	private PlannersAdapter plannersAdapter;
	private int characterID;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.planners);

		final int characterID = getIntent().getIntExtra("characterID", 0);
		this.characterID = characterID;

		ListView listView = (ListView) findViewById(R.id.LVPlanners);
		plannerManager = SheetView.getPlannerManager();
		plannersAdapter = new PlannersAdapter(this, listView, plannerManager, characterID);

		Button addButton = (Button) findViewById(R.id.BPlanners);
		addButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				showDialog(DIALOG_ADD);
			}
		});
	}

	protected Dialog onCreateDialog(int id) {
		final Dialog dialog;
		switch (id) {
		case DIALOG_ADD:
			dialog = new Dialog(this);
			dialog.setTitle("New Plan");
			final LinearLayout layout = new LinearLayout(this);
			layout.setOrientation(LinearLayout.VERTICAL);

			final TextView textView = new TextView(this);
			textView.setText("Name:");
			layout.addView(textView);

			final EditText editText = new EditText(this);
			editText.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
			layout.addView(editText);

			final Button button = new Button(this);
			button.setText("Add");
			button.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
			button.setGravity(Gravity.CENTER);
			button.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					plannerManager.createPlanner(editText.getText().toString(), characterID);
					plannersAdapter.notifyDataSetChanged();
					dialog.dismiss();
				}
			});
			layout.addView(button);

			dialog.setContentView(layout);
			return dialog;
		}
		return null;
	}

	@Override
	protected void onResume() {
		super.onResume();
		plannersAdapter.notifyDataSetChanged();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, EveInfoMenu.MENU_CHAR_SKILLS, 0, EveInfoMenu.MENU_CHAR_SKILLS_NAME)
				.setIcon(R.drawable.icon22_41_32);
		menu.add(0, EveInfoMenu.MENU_CERTIFICATES, 0, EveInfoMenu.MENU_CERTIFICATES_NAME).setIcon(
				R.drawable.icon79_01_32);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case EveInfoMenu.MENU_CHAR_SKILLS:
			ActivityLauncher.displaySkills(this, characterID);
			return true;
		case EveInfoMenu.MENU_CERTIFICATES:
			ActivityLauncher.displayCategories(this, characterID);
			return true;
		default:
			return false;
		}
	}
}
